package Q1;

import java.io.Serializable;
import java.util.Objects;

public class Student implements Serializable {
    private static final long serialVersionUID = -5849117361278319089L;
    private int ID;
    private String name;
    private int age;

    public Student(int ID, String name, int age) throws IDException, AgeException {
        setID(ID);
        setName(name);
        setAge(age);
    }

    public Student() {
    }

    public int getID() {
        return ID;
    }

    public void setID(int ID) throws IDException {
        if (ID > 0) {
            this.ID = ID;
        } else {
            throw new IDException("ID值必须大于0");
        }
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) throws AgeException {
        if (age < 0) {
            throw new AgeException("年龄必须大于零");
        } else if (age > 200) {
            throw new AgeException("超200岁也太老了吧");
        } else {
            this.age = age;
        }
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Student student = (Student) o;
        return ID == student.ID &&
                age == student.age &&
                Objects.equals(name, student.name);
    }

    @Override
    public int hashCode() {
        return Objects.hash(ID, name, age);
    }

    @Override
    public String toString() {
        return "Student{" +
                "ID=" + ID +
                ", name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}

class AgeException extends Exception {
    static final long serialVersionUID = -3387516944124229900L;

    public AgeException() {
        super();
    }

    public AgeException(String message) {
        super(message);
    }
}

class IDException extends Exception {
    static final long serialVersionUID = 13226444124229900L;

    public IDException() {
        super();
    }

    public IDException(String message) {
        super(message);
    }
}